package com.itheima.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;


    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<SetmealDish> collect = setmealDishes.stream().map(item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(collect);
    }

    @Override
    public void removeWithDish(List<Long> ids) {

        LambdaQueryWrapper<Setmeal> setmealLqw = new LambdaQueryWrapper<>();
        setmealLqw.in(Setmeal::getId, ids);
        setmealLqw.eq(Setmeal::getStatus, 1);
        int count = count(setmealLqw);
        if (count > 0) {
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> setmealDishLqw = new LambdaQueryWrapper<>();
        setmealDishLqw.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(setmealDishLqw);

    }

    @Override
    public void updateStatus(int status, List<Long> ids) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId, ids);
        if (status == 0) {
            lqw.eq(Setmeal::getStatus, 1);
        } else {
            lqw.eq(Setmeal::getStatus, 0);
        }
        int count = count(lqw);
        if (count != ids.size()) {
            if (status == 0) {
                throw new CustomException("选中的已有禁用的");
            } else {
                throw new CustomException("选中的已有启用的");
            }
        }
        List<Setmeal> setmeals = listByIds(ids);
        List<Setmeal> collect = setmeals.stream().map(item -> {
            item.setStatus(status);
            return item;
        }).collect(Collectors.toList());
        updateBatchById(collect);
    }

    @Override
    public SetmealDto getByIdWithDish(Long id) {
        Setmeal setmeal = getById(id);
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getId() != null, SetmealDish::getDishId, setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishService.list(lqw);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto;
    }

    @Override
    public void updateWithFlavor(SetmealDto setmealDto) {
        updateById(setmealDto);
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(lqw);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<SetmealDish> collect = setmealDishes.stream().map((SetmealDish setmealDish) -> {
            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(collect);
    }
}
